local mt = {}

local function clear_color(str)
	str = str:gsub('(|[cC]%w%w%w%w%w%w%w%w)(.-)|[rR]', function(color, str)
		return str
	end)
	return str
end

function mt:save(datas, config_data)
	for key, tbl in pairs(config_data) do
		self:save_data(datas, tbl)
	end
end

local function removeEmptyLines(input)
	local lines = {}
	for line in input:gmatch("[^\r\n]+") do
		if line:match("%S") then
			table.insert(lines, line)
		end
	end
	return table.concat(lines, "\n")
end

function mt:save_data(datas, path)
	local function is_valid_identifier(str)
		return string.match(str, "^[%a_][%w_]*$") ~= nil
	end

	local function is_integer(str)
		return tonumber(str) and string.match(str, "^%d+$") ~= nil
	end

	local function table_to_string(datas, indent)
		if not indent then indent = 0 end
		local toprint = "{\n"
		indent = indent + 4

		local list = {}
		for key, value in pairs(datas) do
			list[#list + 1] = { key, value }
		end
		table.sort(list, function(a, b)
			if tonumber(a[1]) and tonumber(b[1]) then
				return tonumber(a[1]) < tonumber(b[1])
			end
			return tostring(a[1]) < tostring(b[1])
		end)
		for _, data in ipairs(list) do
			local k, v = table.unpack(data)
			local k_str = tostring(k)
			if k_str ~= nil then
				k = clear_color(k_str)
			end
			local key
			if type(k) == "string" and is_integer(k) then
				key = "[" .. tonumber(k) .. "] = "
			elseif type(k) == "string" and is_valid_identifier(k) then
				key = k .. " = "
			else
				key = "[" .. string.format("%q", k) .. "] = "
			end

			local formatting = string.rep(" ", indent) .. key
			if type(v) == "table" then
				--如果是个空表 就别执行了
				if next(v) ~= nil then
					toprint = toprint .. formatting .. table_to_string(v, indent)
				end
			elseif type(v) == "string" and is_integer(v) then
				toprint = toprint .. formatting .. tonumber(v) .. ",\n"
			elseif type(v) == "boolean" then
				toprint = toprint .. formatting .. tostring(v) .. ",\n"
			elseif type(v) == "number" then
				toprint = toprint .. formatting .. v .. ",\n"
			elseif type(v) == "string" then
				toprint = toprint .. formatting .. string.format("%q", v) .. ",\n"
			end
		end

		-- ::continue::
		toprint = toprint .. string.rep(" ", indent - 4) .. "},\n"
		return toprint
	end

	local buffer = {}
	if datas then
		local list = {}
		for key, value in pairs(datas) do
			list[#list + 1] = { key, value }
		end
		table.sort(list, function(a, b)
			if tonumber(a[1]) and tonumber(b[1]) then
				return tonumber(a[1]) < tonumber(b[1])
			end
			return tostring(a[1]) < tostring(b[1])
		end)
		for _, data in ipairs(list) do
			local key, value = table.unpack(data)
			local str
			if type(key) == 'number' then
				str = "[" .. key .. "] = "
			elseif type(key) == 'string' then
				str = "['" .. key .. "'] = "
			end
			if type(value) == 'table' then
				str = str .. table_to_string(value)
			else
				if type(value) == "string" then
					str = str .. '\'' .. value .. '\','
				else
					str = str .. value .. ','
				end
			end
			str = removeEmptyLines(str)
			table.insert(buffer, str)
		end
	end

	print('生成', (path), IO.Write(path, 'return {\n' .. table.concat(buffer, '\n') .. '\n}'))
end

return mt
